import {createApp} from 'vue'
import {createPinia} from 'pinia'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
import './assets/styles/index.css'
import {getToken} from './utils/storage'

// 检查登录状态
const token = getToken();

// 调试目的：清除可能存在的token
// removeToken();

const app = createApp(App)

// 全局错误处理
app.config.errorHandler = (err, instance, info) => {
    console.error('全局错误:', err)
}

// 注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
    locale: zhCn,
    size: 'default'
})
app.use(Antd)

// 将Vue应用实例挂载到全局，用于未授权时优雅跳转
declare global {
    interface Window {
        __vue_app?: {
            $router: typeof router;
        };
    }
}

window.__vue_app = {
    $router: router
}

app.mount('#app')
